<template>
  <div class="app-container">
    <el-button type="primary" plain @click="add">新增</el-button>
    <div style="padding: 10px" />

    <el-table
      v-loading="listLoading"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="角色名称" align="center" prop="djRoleName" />
      <el-table-column label="权限字符" align="center" prop="permission" />
      <el-table-column label="显示顺序" align="center" prop="djRoleSort" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="edit(scope.row.djRoleId)"
          >
            编辑
          </el-button>
          <el-button size="mini" type="danger" plain @click="remove(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px" />
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100]"
      :current-page="pageNum"
      :page-size="pageSize"
      :total="total"
      @size-change="onPageSizeChange"
      @current-change="onCurrentPageChange"
    />

    <el-dialog
      width="350px"
      :title="title"
      :visible.sync="visible"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="6em">
        <el-form-item label="角色名称" prop="djRoleName">
          <el-input v-model="form.djRoleName" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="权限字符" prop="permission">
          <el-select
            style="width: 100%"
            v-model="form.permission"
            placeholder="请选择权限字符"
          >
            <el-option value="superAdmin" label="superAdmin" />
            <el-option value="admin" label="admin" />
            <el-option value="common" label="common" />
          </el-select>
        </el-form-item>
        <el-form-item label="显示排序" prop="djRoleSort">
          <el-input-number
            style="width: 100%"
            v-model="form.djRoleSort"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item style="margin-bottom: 0">
          <el-button @click="visible = false">关 闭</el-button>
          <el-button type="primary" @click="submit">提 交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRolePage,
  getRoleDetail,
  addRole,
  editRole,
  deleteRole,
} from "@/api2/partyManage";
import moment from "moment";

export default {
  data() {
    return {
      tableData: [],
      listLoading: true,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      visible: false,
      title: "",
      form: {},
      rules: {
        permission: [
          { required: true, message: "权限字符不能为空", trigger: "change" },
        ],
        djRoleName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
        djRoleSort: [
          { required: true, message: "显示排序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取格式化时间字符串
    getFormatedTimeStr(stamp, str = "MM-DD HH:mm") {
      return moment(stamp).format(str);
    },
    getList() {
      this.listLoading = true;
      getRolePage({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }).then((data) => {
        this.listLoading = false;
        this.tableData = data.data;
        this.total = data.total;
      });
    },
    onPageSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
    },
    onCurrentPageChange(val) {
      this.pageNum = val;
      this.getList();
    },
    add() {
      this.title = "新增";
      this.visible = true;
      this.resetForm();
      this.form = {};
    },
    async edit(id) {
      this.title = "编辑";
      this.visible = true;
      const res = await getRoleDetail(id);
      this.form = res.data;
    },
    submit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.form.djRoleId) {
            const { djRoleId, djRoleName, permission, djRoleSort } = this.form;
            const params = { djRoleId, djRoleName, permission, djRoleSort };
            await editRole(params);
            this.$message.success("修改成功");
          } else {
            await addRole(this.form);
            this.$message.success("新增成功");
          }
          this.visible = false;
          this.getList();
        }
      });
    },
    resetForm() {
      if (!this.$refs.form) return;
      this.$refs.form.resetFields();
    },
    remove(row) {
      this.$confirm("是否删除名称为 " + row.djRoleName + " 的数据项?", {
        type: "warning",
      })
        .then(async () => {
          await deleteRole(row.djRoleId);
          this.$message.success("删除成功");
          this.getList();
        })
        .catch(() => {});
    },
  },
};
</script>

